package com.cashmate.hibernate.pojo;
// Generated May 28, 2011 9:04:20 PM by Hibernate Tools 3.2.1.GA

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;
import org.apache.struts2.json.annotations.JSON;

/**
 * Additionalfund generated by hbm2java
 */
@Entity
@Table(name = "loanverification", catalog = "cashmate")
public class Loanverification implements java.io.Serializable {

    private Integer loanverificationid;
    private Employees employees;
    private Products products;
    private Borrowers borrowers;
    private String loantype;
    private String loanverificationcode;
    private int loanterm;
    private Documents documents;
    private int amountapplied;  
    private int creditrating;
    private int amountofstocks;
    private String status;
    private Loans previousloan;
    private int amountrequested;
    private String releasedate;
    private String coborrowerfirstname;
    private String coborrowermiddlename;
    private String coborrowerlastname;
    private String coborrowerstreetaddress;
    private String coborrowerbarangay;
    private String coborrowercityormunicipality;
    private String coborrowerprovince;
    private String businesstype;
//    private String typeofcollaterals;
    private Request request;
    //custom
    private Creditmatrix creditmatrix;
    private String auditor;
    private String collaterals;
    private String branchname;
    private String borrowername;
    private String areaname;
    private String documentno;
    private String documentdate;
    private String withci;
    private String withap;
    private String withaf;
    private String withcm;
    private String requestname;
    private String requestDocumentNo;
    private Set<Creditinvestigations> creditinvestigationses = new HashSet<Creditinvestigations>(0);
    private Set<Creditmatrix> creditmatrixes = new HashSet<Creditmatrix>(0);
    private Set<Appraisal> appraisals = new HashSet<Appraisal>(0);
    private Set<Loanapplications> loanapplicationses = new HashSet<Loanapplications>(0);

    public Loanverification() {
    }

    public Loanverification(Documents documents, Borrowers borrowers, Employees employees, String loantype, Products products, String loanverificationcode, int loanterm, int amountapplied, String status, Set<Appraisal> appraisals, Set<Creditmatrix> creditmatrixes, Set<Creditinvestigations> creditinvestigationses, Set<Loanapplications> loanapplicationses, int amountrequested, String releasedate, Request request) {
        this.borrowers = borrowers;
        this.loantype = loantype;
        this.products = products;
        this.employees = employees;
        this.loanverificationcode = loanverificationcode;
        this.loanterm = loanterm;
        this.amountapplied = amountapplied;
        this.documents = documents;
        this.creditinvestigationses = creditinvestigationses;
        this.appraisals = appraisals;
        this.creditmatrixes = creditmatrixes;
        this.loanapplicationses = loanapplicationses;
        this.amountrequested = amountrequested;
        this.releasedate = releasedate;
        this.status = status;
        this.request = request;
    }

    public Loanverification(Documents documents, Borrowers borrowers, Employees employees, String loantype, Products products, String loanverificationcode, int loanterm, int amountapplied, int amountrequested, String releasedate) {
        this.borrowers = borrowers;
        this.loantype = loantype;
        this.products = products;
        this.employees = employees;
        this.loanverificationcode = loanverificationcode;
        this.amountrequested = amountrequested;
        this.loanterm = loanterm;
        this.amountapplied = amountapplied;
        this.documents = documents;
        this.releasedate = releasedate;
    }

    public Loanverification(Documents documents, Borrowers borrowers, Employees employees, String loantype, Products products, int loanterm, int amountapplied, Request request) {
        this.borrowers = borrowers;
        this.loantype = loantype;
        this.products = products;
        this.employees = employees;
        this.loanterm = loanterm;
        this.amountapplied = amountapplied;
        this.documents = documents;
        this.request = request;
    }

    public Borrowers getBorrowers() {
        return borrowers;
    }

    public void setBorrowers(Borrowers borrowers) {
        this.borrowers = borrowers;
    }

    public Products getProducts() {
        return products;
    }

    public void setProducts(Products products) {
        this.products = products;
    }
    
    public Request getRequest() {
        return this.request;
    }

    public void setRequest(Request request) {
        this.request = request;
    }

    public Employees getEmployees() {
        return this.employees;
    }

    public void setEmployees(Employees employees) {
        this.employees = employees;
    }

    public Documents getDocuments() {
        return this.documents;
    }

    public void setDocuments(Documents documents) {
        this.documents = documents;
    }

    public Loans getPreviousloan() {
        return previousloan;
    }

    public void setPreviousloan(Loans previousloan) {
        this.previousloan = previousloan;
    }
    
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "loanverificationid", unique = true, nullable = false)
    public Integer getLoanverificationid() {
        return loanverificationid;
    }

    public void setLoanverificationid(Integer loanverificationid) {
        this.loanverificationid = loanverificationid;
    }

    @Column(name = "loantype", nullable = false, length = 45)
    public String getLoantype() {
        return loantype;
    }

    public void setLoantype(String loantype) {
        this.loantype = loantype;
    }

    @Column(name = "withci", nullable = false, length = 45)
    public String getWithci() {
        return withci;
    }

    public void setWithci(String withci) {
        this.withci = withci;
    }

    @Column(name = "withap", nullable = false, length = 45)
    public String getWithap() {
        return withap;
    }

    public void setWithap(String withap) {
        this.withap = withap;
    }

    @Column(name = "withaf", nullable = false, length = 45)
    public String getWithaf() {
        return withaf;
    }

    public void setWithaf(String withaf) {
        this.withaf = withaf;
    }

    @Column(name = "withcm", nullable = false, length = 45)
    public String getWithcm() {
        return withcm;
    }

    public void setWithcm(String withcm) {
        this.withcm = withcm;
    }

    @Column(name = "status", nullable = false, length = 45)
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Column(name = "loanterm", nullable = false)
    public int getLoanterm() {
        return loanterm;
    }

    public void setLoanterm(int loanterm) {
        this.loanterm = loanterm;
    }

    @Column(name = "amountapplied", nullable = false)
    public int getAmountapplied() {
        return amountapplied;
    }

    public void setAmountapplied(int amountapplied) {
        this.amountapplied = amountapplied;
    }
    
    @Column(name = "amountrequested", nullable = true)
    public int getAmountrequested() {
        return amountrequested;
    }

    public void setAmountrequested(int amountrequested) {
        this.amountrequested = amountrequested;
    }
    
    @Column(name = "creditrating", nullable = true)
    public int getCreditrating() {
        return creditrating;
    }

    public void setCreditrating(int creditrating) {
        this.creditrating = creditrating;
    }
    
    @Column(name = "amountofstocks", nullable = true)
    public int getAmountofstocks() {
        return amountofstocks;
    }

    public void setAmountofstocks(int amountofstocks) {
        this.amountofstocks = amountofstocks;
    }
    
    @Column(name = "coborrowerfirstname", nullable = false, length = 45)
    public String getCoborrowerfirstname() {
        return coborrowerfirstname;
    }

    public void setCoborrowerfirstname(String coborrowerfirstname) {
        this.coborrowerfirstname = coborrowerfirstname;
    }
    
    @Column(name = "coborrowermiddlename", nullable = false, length = 45)
    public String getCoborrowermiddlename() {
        return coborrowermiddlename;
    }

    public void setCoborrowermiddlename(String coborrowermiddlename) {
        this.coborrowermiddlename = coborrowermiddlename;
    }
    
    @Column(name = "coborrowerlastname", nullable = false, length = 45)
    public String getCoborrowerlastname() {
        return coborrowerlastname;
    }

    public void setCoborrowerlastname(String coborrowerlastname) {
        this.coborrowerlastname = coborrowerlastname;
    }
    
    @Column(name = "coborrowerstreetaddress", nullable = false, length = 45)
    public String getCoborrowerstreetaddress() {
        return coborrowerstreetaddress;
    }

    public void setCoborrowerstreetaddress(String coborrowerstreetaddress) {
        this.coborrowerstreetaddress = coborrowerstreetaddress;
    }
    
    @Column(name = "coborrowerbarangay", nullable = false, length = 45)
    public String getCoborrowerbarangay() {
        return coborrowerbarangay;
    }

    public void setCoborrowerbarangay(String coborrowerbarangay) {
        this.coborrowerbarangay = coborrowerbarangay;
    }
    
    @Column(name = "coborrowercityormunicipality", nullable = false, length = 45)
    public String getCoborrowercityormunicipality() {
        return coborrowercityormunicipality;
    }

    public void setCoborrowercityormunicipality(String coborrowercityormunicipality) {
        this.coborrowercityormunicipality = coborrowercityormunicipality;
    }
    
    @Column(name = "coborrowerprovince", nullable = false, length = 45)
    public String getCoborrowerprovince() {
        return coborrowerprovince;
    }

    public void setCoborrowerprovince(String coborrowerprovince) {
        this.coborrowerprovince = coborrowerprovince;
    }
    
    @Column(name = "businesstype", nullable = false, length = 45)
    public String getBusinesstype() {
        return businesstype;
    }

    public void setBusinesstype(String businesstype) {
        this.businesstype = businesstype;
    }
    
//    @Column(name = "typeofcollaterals", nullable = false, length = 45)
//    public String getTypeofcollaterals() {
//        return typeofcollaterals;
//    }
//
//    public void setTypeofcollaterals(String typeofcollaterals) {
//        this.typeofcollaterals = typeofcollaterals;
//    }

    @Column(name = "loanverificationcode", nullable = true)
    public String getLoanverificationcode() {
        return loanverificationcode;
    }
    
    public void setLoanverificationcode(String loanverificationcode) {
        this.loanverificationcode = loanverificationcode;
    }
    
    @Column(name = "releasedate", nullable = true)
    public String getReleasedate() {
        return releasedate;
    }

    public void setReleasedate(String releasedate) {
        this.releasedate = releasedate;
    }

    @JSON(serialize = false)
    public Set<Creditinvestigations> getCreditinvestigationses() {
        return this.creditinvestigationses;
    }

    public void setCreditinvestigationses(Set<Creditinvestigations> creditinvestigationses) {
        this.creditinvestigationses = creditinvestigationses;
    }

    @JSON(serialize = false)
    public Set<Appraisal> getAppraisals() {
        return this.appraisals;
    }

    public void setAppraisals(Set<Appraisal> appraisals) {
        this.appraisals = appraisals;
    }

    @JSON(serialize = false)
    public Set<Creditmatrix> getCreditmatrixes() {
        return this.creditmatrixes;
    }

    public void setCreditmatrixes(Set<Creditmatrix> creditmatrixes) {
        this.creditmatrixes = creditmatrixes;
    }

    @JSON(serialize = false)
    public Set<Loanapplications> getLoanapplicationses() {
        return this.loanapplicationses;
    }

    public void setLoanapplicationses(Set<Loanapplications> loanapplicationses) {
        this.loanapplicationses = loanapplicationses;
    }
    //custom
    private String businesstypes;
    private String businessaddresses;
    private String firstlast;
    private String secondlast;
    private int firstlastpn;
    private int secondlastpn;
    private String borroweraddress;
    private String applicationdate;
    private String coborrowername;

    public String getApplicationdate() {
        applicationdate = documentdate;
        return applicationdate;
    }

    public String getCoborrowername() {
        coborrowername = coborrowerlastname+","+coborrowerfirstname+" "+coborrowerlastname;
        return coborrowername;
    }
    
    public String getBorroweraddress() {
        borroweraddress = borrowers.getBarangay() + ", " + borrowers.getCityormunicipality() + ", " + borrowers.getProvince();
        return borroweraddress;
    }

    public String getBusinessaddresses() {
        businessaddresses = "";
        if (getLoanapp() != null) {
            Set<Businesses> businesseses = getLoanapp().getBusinesseses();
            Businesses[] arr = new Businesses[businesseses.size()];
            businesseses.toArray(arr);
            ArrayList<Businesses> arrBusiness = new ArrayList();
            for (Businesses b : arr) {
                arrBusiness.add(b);
            }
            java.util.Collections.sort(arrBusiness, new Comparator() {

                @Override
                public int compare(Object o1, Object o2) {
                    Businesses p1 = (Businesses) o1;
                    Businesses p2 = (Businesses) o2;
                    return p1.getBusinessid().compareTo(p2.getBusinessid());
                }
            });

            if (businesseses.size() >= 5) {
                int count = 0;
                while (count < 5) {
                    if (count == 4) {
                        businessaddresses = businessaddresses + arrBusiness.get(count).getCompleteaddress();
                    } else {
                        businessaddresses = businessaddresses + arrBusiness.get(count).getCompleteaddress() + "/";
                    }
                    count++;
                }
            } else if (businesseses.size() < 5) {
                int count = 0;
                while (count < arrBusiness.size()) {
                    if (count == (arrBusiness.size() - 1)) {
                        businessaddresses = businessaddresses + arrBusiness.get(count).getCompleteaddress();
                    } else {
                        businessaddresses = businessaddresses + arrBusiness.get(count).getCompleteaddress() + "/";
                    }
                    count++;
                }
            } else if (businesseses.isEmpty()) {
                businessaddresses = "N/A";
            }
        }
        return businessaddresses;
    }

    public String getBusinesstypes() {
        businesstypes = "";
        if (getLoanapp() != null) {
            Set<Businesses> businesseses = getLoanapp().getBusinesseses();
            Businesses[] arr = new Businesses[businesseses.size()];
            businesseses.toArray(arr);
            ArrayList<Businesses> arrBusiness = new ArrayList();
            for (Businesses b : arr) {
                arrBusiness.add(b);
            }
            java.util.Collections.sort(arrBusiness, new Comparator() {

                @Override
                public int compare(Object o1, Object o2) {
                    Businesses p1 = (Businesses) o1;
                    Businesses p2 = (Businesses) o2;
                    return p1.getBusinessid().compareTo(p2.getBusinessid());
                }
            });

            if (businesseses.size() >= 5) {
                int count = 0;
                while (count < 5) {
                    if (count == 4) {
                        businesstypes = businesstypes + arrBusiness.get(count).getBusinesstype();
                    } else {
                        businesstypes = businesstypes + arrBusiness.get(count).getBusinesstype() + "/";
                    }
                    count++;
                }
            } else if (businesseses.size() < 5) {
                int count = 0;
                while (count < arrBusiness.size()) {
                    if (count == businesseses.size() - 1) {
                        businesstypes = businesstypes + arrBusiness.get(count).getBusinesstype();
                    } else {
                        businesstypes = businesstypes + arrBusiness.get(count).getBusinesstype() + "/";
                    }
                    count++;
                }
            } else if (businesseses.isEmpty()) {
                businesstypes = "N/A";
            }
        }
        return businesstypes;
    }

    public String getFirstlast() {
        firstlast = "";
        if (borrowers.getFirstlast() != null) {
            firstlast = borrowers.getFirstlast().getModeofpayment();
            if (firstlast == null) {
                firstlast = "";
            }
        }
        return firstlast;
    }

    public String getSecondlast() {
        secondlast = "";
        if (borrowers.getSecondlast() != null) {
            secondlast = borrowers.getSecondlast().getModeofpayment();
            if (secondlast == null) {
                secondlast = "";
            }
        }
        return secondlast;
    }

    public int getFirstlastpn() {
        firstlastpn = 0;
        if (borrowers.getFirstlast() != null) {
            firstlastpn = (int) borrowers.getFirstlast().getLoanamount();
        }
        return firstlastpn;
    }

    public int getSecondlastpn() {
        secondlastpn = 0;
        if (borrowers.getSecondlast() != null) {
            secondlastpn = (int) borrowers.getSecondlast().getLoanamount();
        }
        return secondlastpn;
    }

    public Creditmatrix getCreditmatrix() {
        for (Object c : creditmatrixes.toArray()) {
            creditmatrix = (Creditmatrix) c;
        }
        return creditmatrix;
    }

    public String getAuditor() {
        auditor = "";
        for (Object c : creditmatrixes.toArray()) {
            Creditmatrix x = (Creditmatrix) c;
            auditor = x.getEmployees().getCompletename();
        }

        return auditor;
    }

    public String getCollaterals() {
        collaterals = "";
        for (Object c : creditinvestigationses.toArray()) {
            Creditinvestigations creditinv = (Creditinvestigations) c;
            collaterals = creditinv.getCollaterals();
        }

        return collaterals;
    }

    public String getBranchname() {
        branchname = borrowers.getBranches().getBranchname();
        return branchname;
    }

    public String getBorrowername() {
        borrowername = borrowers.getCompletename();
        return borrowername;
    }

    public String getAreaname() {
        areaname = borrowers.getAreacode();
        return areaname;
    }

    public String getDocumentno() {
        if (borrowers != null) {
            documentno = borrowers.getBranches().getBranchcode() + "LA" + String.format("%07d", loanverificationid);
        } else {
            documentno = "n/a";
        }
        return documentno;

    }

    public String getDocumentdate() {
        documentdate = documents.getDocumentdate();
        return documentdate;
    }
    private Loanapplications loanapp;

    public Loanapplications getLoanapp() {
        loanapp = null;
        for (Loanapplications l : getLoanapplicationses()) {
            loanapp = l;
        }
        return loanapp;
    }
    
    public String getRequestname() {
        requestname = "Request No. " + requestDocumentNo;
        return requestname;
    }
    
    public String getRequestDocumentNo() {
        if (request != null && request.getDocuments() != null) {
            requestDocumentNo = request.getDocuments().getDocumentno();
        } else {
            requestDocumentNo = "";
        }
        return requestDocumentNo;
    }
}
